package com.jxlg.action;

import java.security.NoSuchAlgorithmException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;

import org.hibernate.Transaction;

import com.jxlg.bean.DOperator;
import com.jxlg.dao.DOperatorDAO;
import com.jxlg.util.HibernateSessionFactory;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6949195730494350508L;
	private String czybh;// 操作员编号
	private String czymm;// 操作员密码

	public String getCzybh()
	{
		return czybh;
	}

	public void setCzybh(String czybh)
	{
		this.czybh = czybh;
	}

	public String getCzymm()
	{
		return czymm;
	}

	public void setCzymm(String czymm)
	{
		this.czymm = czymm;
	}

	public String login() throws SecurityException, IllegalStateException,
			RollbackException, HeuristicMixedException,
			HeuristicRollbackException, SystemException,
			NoSuchAlgorithmException
	{
		System.out.printf("进入 login() czybh = %s czymm = %s -\n", czybh, czymm);
		Transaction tr = HibernateSessionFactory.getSession()
				.beginTransaction();
		DOperatorDAO dao = new DOperatorDAO();

		DOperator operator = dao.findByCzybh(czybh);
		tr.commit();

		if (null == operator)
		{
			this.addFieldError("loginErrors", "操作员编号填写错误！");
			return Action.INPUT;
		} else
		{
			String get_czymm = operator.getCzymm();
			String get_czyxm = operator.getCzyxm();
			System.out.printf("操作员姓名: %s\n", get_czyxm);
			System.out.printf("数据库密码: %s\n", get_czymm);
			String get_czymm_hash = null;
			get_czymm_hash = com.lemox.security.HashCode.sha1sum(get_czymm);
			System.out.printf("数据库密码hash值: %s\n", get_czymm_hash);
			System.out.printf("传 输   密 码hash值: %s\n", czymm);

			/**
			 * hash值是否匹配
			 */
			if (get_czymm_hash.equals(czymm))
			{
				System.out.println("客户端登陆成功");
			} else
			{
				System.out.println("密码不匹配，拒绝登录。");
				return "wrong_password";
			}
		}

		// 得到ActionContext实例
		ActionContext context = ActionContext.getContext();
		// 将（name，“request：tom”）到ActionContext中
		context.put("name", "request:tom");
		// 得到application并将（name，“application：tom”）放入
		context.getApplication().put("name", "application:tom");
		// 得到session并将（name，“session：tom”）放入
		context.getSession().put("operator", operator);

		int _operator_jb_ = operator.getJb();
		System.out.println("操作员级别 : " + _operator_jb_);
		if (0 == _operator_jb_)
		{
			/**
			 * 如果是jb是0，也就是系统管理员的话，进入领导审批页面。
			 */
			return "go_ldsp";
		} else
		{
			return "go_rcdj";
		}
	}

	public void validate()
	{
		System.out.println("validate 登录用户代码：" + czybh);
		if (czybh.equals(""))
		{
			// this.addActionError("ActionError操作员编号不能为空！");
			// this.clearFieldErrors();
			this.addFieldError("loginErrors", "操作员编号不能为空！");
			return;
		}
	}

	public void validateLogin()
	{
		System.out.println("validateLogin 登录用户代码：" + czybh);
		if (czybh.equals(""))
		{
			// this.addActionError("ActionError操作员编号不能为空！");
			// this.clearFieldErrors();
			this.addFieldError("loginErrors", "操作员编号不能为空！");
			return;
		}
	}

	public void validateLogin2()
	{
		System.out.println("validateLogin2 登录用户代码：" + czybh);
		if (czybh.equals(""))
		{
			// this.addActionError("ActionError操作员编号不能为空！");
			// this.clearFieldErrors();
			this.addFieldError("loginErrors", "操作员编号不能为空！");
			return;
		}
	}
}
